var mongoose = require('mongoose')

mongoose.Promise = global.Promise

mongoose.connect("mongodb://localhost/blog_shop")

var Schema = mongoose.Schema

var blogTypeSchema = new Schema({
    name: String,
    code: String
})

var BlogType = mongoose.model("blog_type", blogTypeSchema)

var db_blog_type = {}

//查询博客的分类表
db_blog_type.getData = function(searchName, callback) {
    var filter = {}
    if (searchName) {
        filter.name = new RegExp(searchName, "i")
    }
    BlogType.find(filter)
        .sort({ _id: -1 })
        .then(res => {
            callback(res)
        })
        .catch(err => {
            console.log(err)
        })
}

db_blog_type.save = function(model, callback) {
    var data = new BlogType(model)
    data.save()
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}

db_blog_type.del = function(id, callback) {
    BlogType.findByIdAndRemove(id)
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}

db_blog_type.findById = function(id, callback) {
    BlogType.findById(id)
        .then(res => {
            callback(res)
        })
        .catch(err => {
            console.log(err)
            callback(null)
        })
}

db_blog_type.updateById = function(id, model, callback) {
    BlogType.findByIdAndUpdate(id, model)
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}







var blogSchema = new Schema({
    title: String,
    code: String,
    jianjie: String,
    crux: String,
    content: String,
})

var BlogContent = mongoose.model("blog", blogSchema)

var db_blog = {}

//查询博客的分类表
db_blog.getData = function(searchName, callback) {
    var filter = {}
    if (searchName) {
        filter.code = new RegExp(searchName, "i")
    }
    BlogContent.find(filter)
        .sort({ _id: -1 })
        .then(res => {
            callback(res)
        })
        .catch(err => {
            console.log(err)
        })
}

db_blog.save = function(model, callback) {
    var data = new BlogContent(model)
    data.save()
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}

db_blog.del = function(id, callback) {
    BlogContent.findByIdAndRemove(id)
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}

db_blog.findById = function(id, callback) {
    BlogContent.findById(id)
        .then(res => {
            callback(res)
        })
        .catch(err => {
            console.log(err)
            callback(null)
        })
}

db_blog.updateById = function(id, model, callback) {
    BlogContent.findByIdAndUpdate(id, model)
        .then(callback(true))
        .catch(err => {
            console.log(err)
            callback(false)
        })
}


var db = {}
db.dal_blog_type = db_blog_type //导出blog_type相关操作
db.dal_blog = db_blog
module.exports = db